package leetcode;

import java.util.ArrayList;
import java.util.List;

public class LC27 {
    public static void main(String[] args) {

    }
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode ans = null;
        ListNode temp = null;
        while(list1 != null && list2 != null){
            if(list1.val > list2.val) {
                if (ans == null) {ans = list2; temp = ans;}
                else {temp.next = list2; temp = temp.next;}
                list2 = list2.next;
            }else {
                if (ans == null) { ans = list1; temp = ans;}
                else {temp.next = list1; temp = temp.next;}
                list1 = list1.next;
            }
        }
        if (list1 == null){
                if (ans == null) {ans = list2;}
                else {temp.next = list2;}
        }else {
            if (ans == null) { ans = list1; }
            else {temp.next = list1;}
        }
        return ans;
    }
}
